<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null" style="margin-bottom: 70px">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <a-row>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="法人行"
              :labelCol="{span: 5}"
              :wrapperCol="{span: 16, offset: 1}">
              <a-select
                        mode="default"
                        :allowClear="true"
                        style="width: 100%"
                        @change="handleInstChange"
                        v-model="queryParams.instId">
                <a-select-option v-for="r in instIdData" :key="r.label">{{r.label}}-{{r.value}}</a-select-option>
              </a-select>
<!--              <a-input v-if="!this.user.admin" v-model="queryParams.instId" :value="this.user.instId"
                       disabled="disabled"/>-->
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24" v-show="queryParams.instId!=''">
            <a-form-item
              label="对账场景标识"
              :labelCol="{span: 5}"
              :wrapperCol="{span: 16, offset: 1}">
              <a-input placeholder="对账场景标识" v-model="queryParams.reconSceneId"/>
            </a-form-item>
          </a-col>

        </a-row>
        <div v-if="queryParams.instId!=''" style=" margin-top: 3px; position: relative;left:585px;top:45px;margin-top: 40px;">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </div>
      </a-form>
    </div>
    <div v-show="queryParams.instId!=''">
      <div class="operator" style="height: 50px">
        <a-button v-hasPermission="['reconscene:add']" type="primary" @click="add" v-show="true">新增</a-button>
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="(record,index) => {return index}"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :scroll="{ x: 1800 }"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               @change="handleTableChange">
        <template slot="operation" slot-scope="text, record">
          <a-icon v-hasPermission="['reconscene:update']" type="setting" style="color: #4a9ff5" v-show="true"
                  @click="edit(record)"
                  title="修改"/>&nbsp;&nbsp;
          <a-icon type="eye" style="color: #42b983" @click="view(record)"
                  title="查看"/>&nbsp;&nbsp;
          <a-popconfirm title="确定停用？" @confirm="reconSceneStop(record)" okText="确定" cancelText="取消">
            <a-icon v-hasPermission="['reconscene:stop']" type="minus-circle" style="color: #42b983" title="停用"/>&nbsp;&nbsp;
          </a-popconfirm>&nbsp;&nbsp;
          <a-popconfirm title="确定启用？" @confirm="reconSceneUse(record)" okText="确定" cancelText="取消">
<!--            <a-icon v-hasPermission="['reconscene:use']" type="use" style="color: #42b983" title="启用"/>&nbsp;&nbsp;-->
            <a-icon v-hasPermission="['reconscene:use']" type="check-circle" theme="twoTone" twoToneColor="#42b983" title="启用"/>
          </a-popconfirm>&nbsp;&nbsp;
          <a-popconfirm title="确定注销？" @confirm="reconSceneCancel(record)" okText="确定" cancelText="取消">
            <a-icon v-hasPermission="['reconscene:cancel']" type="poweroff" style="color: #42b983" title="注销"/>&nbsp;&nbsp;
          </a-popconfirm>&nbsp;&nbsp;
        </template>
      </a-table>
    </div>
    <!-- 新增对账场景状态信息 -->
    <reconScene-input
      ref="ReconSceneInput"
      @success="handleReconSceneAddSuccess"
      @close="handleReconSceneAddClose"
      :reconSceneInputVisiable="reconSceneInputVisiable"
      :reconSceneInputInstId="reconSceneInputInstId">
    </reconScene-input>
    <!-- 修改对账场景状态信息 -->
    <reconScene-edit
      ref="ReconSceneEdit"
      @success="handleReconSceneEditSuccess"
      @close="handleReconSceneEditClose"
      :reconSceneEditVisiable="reconSceneEditVisiable">
    </reconScene-edit>
    <!-- 查看对账场景状态信息 -->
    <reconScene-detail
      ref="ReconSceneDetail"
      @close="handleReconSceneDetailClose"
      :reconSceneDetailVisiable="reconSceneDetailVisiable">
    </reconScene-detail>
  </a-card>
</template>

<script>
  import RangeDate from '../../../../components/datetime/RangeDate';
  import ReconSceneInput from './ReconSceneInput';
  import ReconSceneEdit from './ReconSceneEdit';
  import ReconSceneDetail from './ReconSceneDetail';
  import {mapMutations, mapState} from 'vuex'

  const formItemLayout = {
    labelCol: {span: 5}
  }
  export default {
    name: 'reconScene',
    components: {
      ReconSceneInput,
      ReconSceneEdit,
      ReconSceneDetail,
      RangeDate
    },
    data() {
      return {
        advanced: false,
        dataSource: [],
        formItemLayout,
        selectedRowKeys: [],
        queryParams: {
          instId: '',
          reconSceneId: ''
        },
        filteredInfo: null,
        sortedInfo: null,
        paginationInfo: null,
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false,
        reconSceneInputVisiable: false,
        reconSceneInputInstId: '',
        reconSceneEditVisiable: false,
        reconSceneDetailVisiable: false,
        instIdData: [],


      }
    },

    mounted() {
      this.$get('admin/upmporg/getInstList').then((r) => {
        this.instIdData = r.data
      })
      if(!this.user.admin){
        this.$set(this.queryParams,"instId",this.user.instId);
        this.search();
      }
      // this.fetch()
    },

    computed: {
      ...mapState({
        user: state => state.account.user,

      }),

      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        let {filteredInfo} = this
        filteredInfo = filteredInfo || {}
        return [{
          title: '法人行号',
          dataIndex: 'instId',
          scopedSlots: {customRender: 'instId'}
        }, {
          title: '对账场景标识',
          dataIndex: 'reconSceneId',
          scopedSlots: {customRender: 'reconSceneId'},
        }, {
          title: '已触发的最小文件日期',
          dataIndex: 'minFileDate',
          scopedSlots: {customRender: 'minFileDate'},
        }, {
          title: '节假日是否对账',
          dataIndex: 'reconAtHolday',
          width: 150,
          customRender: (text, row, index) => {
            switch (text) {
              case 'Y':
                return '对账'
              case 'N':
                return '不对账'
              default:
                return text
            }
          },
          filters: [
            {text: '对账', value: 'Y'},
            {text: '不对账', value: 'N'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.reconAtHolday || null,
          onFilter: (value, record) => record.reconAtHolday.indexOf(value) === 0
        }, {
          title: '状态',
          dataIndex: 'status',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return '非正式'
              case '01':
                return '正式'
              case '02':
                return '停用'
              case '03':
                return '已注销'
              default:
                return text
            }
          },
          filters: [
            {text: '非正式', value: '00'},
            {text: '正式', value: '01'},
            {text: '停用', value: '02'},
            {text: '已注销', value: '03'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.status || null,
          onFilter: (value, record) => record.status.indexOf(value) === 0
        }, {
          title: '审核状态',
          dataIndex: 'auditStatus',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return '待提交审批'
              case '01':
                return '新增-审批流程中'
              case '02':
                return '新增-审批通过'
              case '03':
                return '新增-审批拒绝'
              case '11':
                return '注销-审批流程中'
              case '12':
                return '注销-审批通过'
              case '13':
                return '注销-审批拒绝'
              case '21':
                return '停用-审批流程中'
              case '22':
                return '停用-审批通过'
              case '23':
                return '停用-审批拒绝'
              case '31':
                return '启用-审批流程中'
              case '32':
                return '启用-审批通过'
              case '33':
                return '启用-审批拒绝'
              case '41':
                return '修改-审批流程中'
              case '42':
                return '修改-审批通过'
              case '43':
                return '修改-审批拒绝'
              default:
                return text
            }
          },
          filters: [
            {text: '待提交审批', value: '00'},
            {text: '新增-审批流程中', value: '01'},
            {text: '新增-审批通过', value: '02'},
            {text: '新增-审批拒绝', value: '03'},
            {text: '注销-审批流程中', value: '11'},
            {text: '注销-审批通过', value: '12'},
            {text: '注销-审批拒绝', value: '13'},
            {text: '停用-审批流程中', value: '21'},
            {text: '停用-审批通过', value: '22'},
            {text: '停用-审批拒绝', value: '23'},
            {text: '启用-审批流程中', value: '31'},
            {text: '启用-审批通过', value: '32'},
            {text: '启用-审批拒绝', value: '33'},
            {text: '修改-审批流程中', value: '41'},
            {text: '修改-审批通过', value: '42'},
            {text: '修改-审批拒绝', value: '43'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.auditStatus || null,
          onFilter: (value, record) => record.auditStatus.indexOf(value) === 0

        }, {
          title: '创建时间',
          dataIndex: 'createTime',
          scopedSlots: {customRender: 'createTime'},
        }, {
          title: '审核时间通过时间',
          dataIndex: 'auditTime',
          scopedSlots: {customRender: 'auditTime'},
        }, {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: {customRender: 'operation'},
          fixed: 'right',
          width: 180
        }]
      }
    },

    methods: {
      handleReconSceneDetailClose(){
        this.reconSceneDetailVisiable = false
      },
      handleInstChange(value) {
        this.queryParams.instId = value;
        if (value != "") {
          let params = {}
          params.instId = value;
          this.$get('/admin/csmc/reconscene', {...params}).then((r) => {
            this.dataSource = r.data.rows;
            this.search();
          })
        }
      },
      onSelectChange(selectedRowKeys, selectedRow) {
        console.log(selectedRow);
        this.selectedRowKeys = selectedRowKeys
      },
      view(record) {
        this.reconSceneDetailVisiable = true;
        this.$refs.ReconSceneDetail.setFormValues(record, "view");
      },
      handleReconSceneEditClose() {
        this.reconSceneEditVisiable = false
      },
      handleReconSceneEditSuccess(msg) {
        this.reconSceneEditVisiable = false
        this.$message.success(msg)
        this.search()
      },
      handleReconSceneAddClose() {
        this.reconSceneInputVisiable = false
      },
      handleReconSceneAddSuccess(msg) {
        this.reconSceneInputVisiable = false
        this.$message.success(msg)
        this.search()
      },
      add() {
        let data = {};
        data.instId = this.queryParams.instId;
        this.reconSceneInputVisiable = true
        this.reconSceneInputInstId = this.queryParams.instId
        this.$refs.ReconSceneInput.setFormValues(data, "add")
      },
      edit(record) {
        console.log(record)
        if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$confirm({
            title: '提示',
            content: '申请待审批的数据不允许修改',
          });
        } else if (record.status === '02' || record.status === '03') {
          this.$confirm({
            title: '提示',
            content: '停用、注销状态时不能修改',
          });
        } else {
        this.reconSceneEditVisiable = true
        this.$refs.ReconSceneEdit.setFormValues(record, "update")
        }
      },
      handleTableChange(pagination, filters, sorter) {
        // 将这三个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        // 重置列过滤器规则
        this.filteredInfo = filters
        // 重置列排序规则
        this.sortedInfo = sorter

        // this.reconSceneInfo.visiable = false
        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },
      batchDelete() {
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要注销的记录')
          return
        }
        let that = this
        this.$confirm({
          title: '确定注销所选中的记录?',
          content: '当您点击确定按钮后，这些记录将会被注销！',
          centered: true,
          onOk() {
            that.$delete('/admin/csmc/reconscene/' + that.selectedRowKeys.join(',')).then(() => {
              that.$message.success('注销成功')
              that.selectedRowKeys = []
              that.fetch()
            })
          },
          onCancel() {
            that.selectedRowKeys = []
          }
        })
      },
      search() {
        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        let params = {}
        params.instId = this.queryParams.instId
        params.reconSceneId = this.queryParams.reconSceneId

        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...params,
          ...filteredInfo
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置列过滤器规则
        this.filteredInfo = null
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        this.fetch()
      },

      fetch(params = {}) {
        this.loading = true;
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('/admin/csmc/reconscene', {
          ...params
        }).then((r) => {
          console.log(r);
          let data = r.data
          this.loading = false
          this.dataSource = r.data.rows
          const pagination = {...this.pagination}
          pagination.total = data.total
          this.pagination = pagination
        })
      },
      reconSceneStop(record) {
        if ('02' == record.status || '03' == record.status) {
          this.$message.error("当前记录已停用或注销，不能停用")
          return
        }else if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$message.warning('审核流程中的数据不允许停用')
          return;
        } else if (record.auditStatus === '22') {
          this.$message.warning("停用的数据不允许再停用")
          return;
        }
        this.$put("/admin/csmc/reconscene/changeStatus/stop/" + record.id, {}).then(r => {
          this.$message.success("停用成功")
          this.search()
        })
      },
      reconSceneUse(record) {
        if ('02' !== record.status) {
          this.$message.error("当前记录非停用状态，不能启用")
          return
        }else if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$message.warning('审核流程中的数据不允许停用')
          return;
        } else if (record.auditStatus === '32') {
          this.$message.warning("已启用的数据不需再启用")
          return;
        }
        this.$put("/admin/csmc/reconscene/changeStatus/use/" + record.id, {}).then(r => {
          this.$message.success("启用成功")
          this.search()
        })
      },
      reconSceneCancel(record) {
        if (record.status != '01' && record.status != '02') {
          this.$message.warning("非正式、已注销状态不能注销")
          return;
        } else if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$message.warning('审核流程中的数据不允许注销')
          return;
        } else if (record.auditStatus === '03') {
          this.$message.warning('注销通过的数据不允许再注销')
          return;
        }
        this.$put("/admin/csmc/reconscene/changeStatus/cancel/" + record.id, {}).then(r => {
          this.$message.success("注销成功")
          this.search()
        })
      },
    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../../static/less/Common";

  .width_70 {
    width: 70%;
  }
</style>
